home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / ubufox / chrome / ubufox.jar / content / pluginAlternativeOverlay.js < prev    next >
Encoding:
JavaScript  |  2009-04-14  |  9.7 KB  |  298 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is ubufox.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Canonical Ltd.
  18.  * Portions created by the Initial Developer are Copyright (C) 2008
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Alexander Sack <asac@jwsdot.com> - Canonical Ltd.
  23.  *   Arzhel Younsi <xionox@gmail.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. var prefBranch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  40. var pluginManager = Components.classes["@mozilla.org/plugin/manager;1"].getService(Components.interfaces.nsIPluginManager);
  41.  
  42. const APP_ICON_ATTR_NAME = "appHandlerIcon";
  43.  
  44. window.addEventListener("load", showContentList, false);
  45.  
  46. var gBrowser = document.getElementById("content");
  47. var usedMimeTypes = new Array();
  48. var allHandledMimeTypes = new Array();
  49. var filteredMimeTypes = null;
  50.  
  51. for (var item in window.arguments[0].plugins){
  52.   usedMimeTypes.push(window.arguments[0].plugins[item].mimetype);  
  53. }
  54.  
  55. let store = new Object();
  56. for (let i = 0; i < navigator.mimeTypes.length; i++) {
  57.   if(!navigator.mimeTypes.item(i).enabledPlugin)
  58.     continue;
  59.   let mtype = navigator.mimeTypes.item(i).type;
  60.   if(!store[mtype])
  61.     allHandledMimeTypes.push(mtype);
  62.   store[mtype] = "x";
  63. }
  64.  
  65. var filehintNameMap = null;
  66.  
  67.  
  68. function parseFilehintNameText (text)
  69. {
  70.    var map = {};
  71.    var array = new Array();
  72.  
  73.    var start = 0;
  74.    var next  = 0;
  75.    next = text.indexOf("\n", start);
  76.  
  77.    while (next  > -1) {
  78.       array.push(text.substring(start, next));
  79.       start = next+1;
  80.       next = text.indexOf("\n", start);
  81.    }
  82.    array.push(text.substring(start));
  83.  
  84.    var key = null;
  85.    var value = null;
  86.    var line = null;
  87.    var separator = 0;
  88.    var left = null;
  89.    var right = null;
  90.    for (let a = 0; a < array.length; a++) {
  91.       line = array[a];
  92.       separator = line.indexOf(":");
  93.       left = line.substring(0, separator);
  94.       right = line.substring(separator+2);
  95.       if (left == "filehint")
  96.          key = right;
  97.       else if(left == "name") {
  98.          map[key] = right;
  99.       }
  100.    }
  101.    return map;
  102. }
  103.  
  104. function getFilehintNameMap () {
  105.   if (filehintNameMap)
  106.     return filehintNameMap;
  107.  
  108.   var url = "http://localhost/~asac/cgi-bin/plugin-finder.py?op=filehint2name&distributionID=9.04";
  109.   try {
  110.     url = prefBranch.getCharPref("pfs.filehint.url");
  111.   } catch (e) {}
  112.  
  113.   var req = new XMLHttpRequest();
  114.   req.open('GET', url, true); /* 3rd argument, true, marks this as async */
  115.   req.onreadystatechange = function (aEvt) {
  116.     if (req.readyState == 4) {
  117.        if(req.status == 200) {
  118.          filehintNameMap = parseFilehintNameText (req.responseText);
  119.          showContentList();
  120.        }
  121.     }
  122.   };
  123.   req.send(null); 
  124.   return null;
  125. }
  126.  
  127. if (!filehintNameMap)
  128.   getFilehintNameMap();
  129.  
  130. function showContentList(){
  131.   var contentName;
  132.   if(!filteredMimeTypes) {
  133.     if(usedMimeTypes.length <= 0)
  134.       filteredMimeTypes = allHandledMimeTypes;
  135.     else
  136.       filteredMimeTypes = usedMimeTypes;
  137.   }
  138.   var _list = document.getElementById("pluginsList");   
  139.   while (_list.hasChildNodes())
  140.     _list.removeChild(_list.lastChild);
  141.   for each (let mimeType in filteredMimeTypes) {
  142.       if(mimeType != ''){
  143.         let item = document.createElement("richlistitem");
  144.         item.setAttribute("type", mimeType);
  145.         contentName = getDescByMime(mimeType);
  146.         item.setAttribute("typeDescription",contentName);
  147.         item.setAttribute("actionDescription", describePreferredAction(mimeType));
  148.         item.setAttribute(APP_ICON_ATTR_NAME, "plugin");
  149.                 item.setAttribute("typeIcon", "moz-icon://goat?size=" + 16 + "&contentType=" + mimeType);
  150.         _list.appendChild(item);
  151.     }
  152.   }
  153. }
  154.  
  155. function describePreferredAction(mimeType){
  156.   var filehintNameMap = getFilehintNameMap();
  157.  
  158.   var preferredFileName = null;
  159.   try {
  160.     preferredFileName = prefBranch.getCharPref("modules.plugins.mimetype." + mimeType);
  161.   } catch (e) {}
  162.  
  163.   var perfectMatchPlugin = null;
  164.  
  165.   if (preferredFileName) {
  166.     for (let i =0; i < navigator.plugins.length; i++) {
  167.       var plugIn = navigator.plugins.item(i);
  168.       if(preferredFileName == plugIn.filename) {
  169.         perfectMatchPlugin = plugIn;
  170.         break;
  171.       }
  172.     }
  173.   }
  174.  
  175.   var L = navigator.mimeTypes.length;
  176.   for(var i=0; i<L && perfectMatchPlugin == null; i++) {
  177.      if (navigator.mimeTypes.item(i).type == mimeType)
  178.      {
  179.        perfectMatchPlugin = navigator.mimeTypes.item(i).enabledPlugin;
  180.      }
  181.    }
  182.  
  183.   if(!perfectMatchPlugin)
  184.     return "...";
  185.  
  186.   for (betterNameCarrierKey in filehintNameMap) {
  187.      if (perfectMatchPlugin.filename && perfectMatchPlugin.filename.indexOf(betterNameCarrierKey) > -1) {
  188.        return filehintNameMap[betterNameCarrierKey];
  189.      }
  190.   }
  191.  
  192.   if(perfectMatchPlugin.name.indexOf("Shockwave Flash") >= 0 ||
  193.      perfectMatchPlugin.name.indexOf("Windows Media Player") >= 0)
  194.     return perfectMatchPlugin.description;
  195.   return perfectMatchPlugin.name;
  196. }
  197.  
  198. function getDescByMime(aMimeType){
  199.   for(let i = 0; i < navigator.mimeTypes.length; i++) {
  200.      let mimeType = navigator.mimeTypes[i];
  201.      if (mimeType.type == aMimeType && mimeType.description != null && mimeType.description.length > 0)
  202.        return mimeType.description + " (" + aMimeType + ")";
  203.    }
  204.   return aMimeType;  
  205. }
  206.  
  207. function onSelectionChanged() {
  208.   var _list = document.getElementById("pluginsList");
  209.   if (_list.selectedItem)
  210.     _list.setAttribute("lastSelectedType", _list.selectedItem.getAttribute("type"));
  211. }
  212.  
  213. function onSelectUsedPlugins (e) {
  214.     filteredMimeTypes = usedMimeTypes;
  215.     showContentList();
  216. }
  217.  
  218. function onSelectAllPlugins (e) {
  219.     filteredMimeTypes = allHandledMimeTypes;
  220.     showContentList();
  221. }
  222.    
  223. var gApplicationsPane = {  
  224.  
  225.   rebuildActionsMenu: function() {
  226.     var filehintNameMap = getFilehintNameMap();
  227.     var _list = document.getElementById("pluginsList");
  228.     var typeItem = _list.selectedItem;
  229.     var menu = document.getAnonymousElementByAttribute(typeItem, "class", "actionsMenu");
  230.     var menuPopup = menu.menupopup;
  231.     var pluginArrayLength = navigator.plugins.length;
  232.     
  233.     while (menuPopup.hasChildNodes())
  234.       menuPopup.removeChild(menuPopup.lastChild);
  235.  
  236.     var menuItem = null;
  237.     for each (let mimeType in filteredMimeTypes) {
  238.       for (var i = 0; i < pluginArrayLength; i++) {
  239.         var plugin = navigator.plugins[i];
  240.         for (var j = 0; j < plugin.length; j++) {
  241.           if(plugin.item(j).type == mimeType && typeItem.type == mimeType){  
  242.             menuItem = document.createElement("menuitem");
  243.             var pluginName = plugin.name;
  244.             if(pluginName.indexOf("Shockwave Flash") >= 0)
  245.               pluginName = plugin.description;
  246.             for (betterNameCarrierKey in filehintNameMap) {
  247.                if (plugin.filename && plugin.filename.indexOf(betterNameCarrierKey) > -1) {
  248.                   pluginName = filehintNameMap[betterNameCarrierKey];
  249.                }
  250.             }
  251.             menuItem.setAttribute("label", pluginName);
  252.             menuItem.setAttribute("id", pluginName);
  253.             menuItem.ubufoxPluginFilename = plugin.filename;
  254.             menuItem.setAttribute(APP_ICON_ATTR_NAME, "plugin");
  255.             menuPopup.appendChild(menuItem);
  256.           }
  257.         }
  258.       }
  259.     }
  260.     var strbundle = document.getElementById("ubufox-alt-strings")
  261.     var search = strbundle.getString("ubufox.altplugins.search");
  262.  
  263.     menuItem = document.createElement("menuitem");
  264.     menuItem.setAttribute("id", "install-plugin-handler");
  265.     menuItem.setAttribute("label", search);
  266.     menuItem.type = typeItem.type;
  267.     menuPopup.appendChild(menuItem);
  268.   },
  269.   
  270.   onSelectAction: function(aActionItem) {
  271.     var _list = document.getElementById("pluginsList");
  272.     var typeItem = _list.selectedItem;
  273.  
  274.     var id = aActionItem.getAttribute ("id");
  275.     if (id == "install-plugin-handler") {
  276.       var pluginInfoArray = {};
  277.       var fakePluginInfo = new Object();
  278.       fakePluginInfo.mimetype = typeItem.type;
  279.       fakePluginInfo.pluginsPage = "";
  280.       pluginInfoArray[typeItem.type] = fakePluginInfo;
  281.  
  282.       window.openDialog("chrome://mozapps/content/plugins/pluginInstallerWizard.xul",
  283.                  "PFSWindow", "chrome,centerscreen,resizable=yes,width=600,height=600",
  284.                  {plugins: pluginInfoArray, browser: gBrowser});
  285.  
  286.       pluginManager.reloadPlugins(true);
  287.       showContentList();
  288.     }
  289.     else {
  290.       prefBranch.setCharPref("modules.plugins.mimetype." + typeItem.type, aActionItem.ubufoxPluginFilename);
  291.       pluginManager.reloadPlugins(true);
  292.       showContentList();
  293.     }
  294.     return;
  295.   }
  296. };
  297.  
  298.